﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;
namespace Web
{
    public partial class admin_indexlink : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindNotice();
            }
            BindData();
        }
        private void BindData()
        {
            BindIndexPic();
        }

        private void BindIndexPic()
        {
            IEnumerable<LinksInfo> links = LinksInfoLogic.GetLinksInfo();
            IndexPicRpt.DataSource = links;
            IndexPicRpt.DataBind();
        }

        private void BindNotice()
        {
            noticefail.Visible = false;
            noticesuccess.Visible = false;
        }
        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "AjaxChange();divupload();", true);
        }

        public void IndexPicManager()
        {
            int flag1 = 1, flag2 = 1;

            if (Request.Form["picurl[]"] != null)
            {
                flag1 = 0;
                string[] picurl = Request.Form["picurl[]"].Split(',');
                string[] picorder = Request.Form["picorder[]"].Split(',');
                string[] piclink = Request.Form["piclink[]"].Split(',');
                string[] picid = Request.Form["picid[]"].Split(',');
                int count = picurl.Count();
                int addsuccess = 0;
                for (int i = 0; i < count; i++)
                {
                    //
                    LinksInfo link = new LinksInfo();
                    link.LinkPicURL = picurl[i];
                    link.LinkURL = piclink[i];
                    link.PicLevel = Convert.ToInt32(picorder[i]);
                    link.LinkID = Convert.ToInt32(picid[i]);
                    if (link.LinkID > 0)
                    {
                        addsuccess += LinksInfoLogic.UpdateLinksInfo(link);
                    }
                    else {
                        addsuccess += LinksInfoLogic.InsertLinksInfo(link);
                    }
                    
                }
                if (addsuccess == count)
                {
                    flag1 = 1;
                }
            }
            if (Request.Form["delete[]"] != null)
            {
                flag2 = 0;
                string[] delete = Request.Form["delete[]"].Split(',');
                int count = delete.Count();
                int delsuccess = 0;
                for (int i = 0; i < count; i++)
                {
                    LinksInfo link = new LinksInfo();
                    link.LinkID = Int32.Parse(delete[i]);
                    delsuccess += LinksInfoLogic.DeleteLinksInfo(link);
                }
                if (delsuccess == count)
                {
                    flag2 = 1;
                }
            }

            if (flag1 == 1 && flag2 == 1)
            {
                ChangeStatus(true);
                BindIndexPic();
            }
            else
            {
                ChangeStatus(false);
            }
        }
        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            IndexPicManager();
        }
    }
}
